import router from '@/router';
import authApi from '@/api/auth';
import store from '@/store/index';
let timer;
const timeout = 5 * 60 * 1000; // 5分钟

function resetTimer() {
    clearTimeout(timer);
    timer = setTimeout(async () => {
        if (!store().isLogin) return;
        console.warn('用户无操作，自动登出');
        router.push('/login');

        await authApi.logout(); // 调用前端注销接口
        store().clearToken();
        store().$patch({ isLogin: false });
    }, timeout);
}

export function setupInactivityWatcher() {
    const events = ['mousemove', 'keydown', 'mousedown', 'touchstart', 'scroll'];
    events.forEach(event => {
        window.addEventListener(event, resetTimer);
    });

    resetTimer(); // 初始启动
}
